Extension Methods
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
Extension methods are static methods, but they can be called as if they were instance methods on the extended type.
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
They can be called directly on the instance or as a normal static method.
using ExtensionMethods;
string s = "Hello Extension Methods";
int i = s.WordCount();
int i = MyExtensions.WordCount(s);
The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T>
types.
To use the standard query operators, first bring them into scope with a using System.Linq
directive. Then any type that implements IEnumerable<T>
appears to have instance methods such as GroupBy
, OrderBy
, Average
, and so on.
You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable<T>
type such as List<T>
or Array
.